﻿using UnityEngine;

namespace UI
{
    
    public delegate string FormNameHandler(string formName);

    internal static class UIFormLoader
    {
        public static UIForm LoadForm(string formName)
        {
            var root = UISystem.Settings.UIFormRootPath;
            var path = formName;
            if (!string.IsNullOrEmpty(root))
            {
                path = $"{root}/{path}";
            }
            var go = UISystem.LoadAsset<GameObject>(path);
            if (go != null)
            {
                go.SetActive(false);
                var instance = Object.Instantiate(go);
                instance.name = formName;
                var element = instance.GetComponent<UIElement>();
                if (element == null)
                {
                    element = instance.AddComponent<UIElement>();
                }
                return new UIForm(formName, UIFormInfos.GetInfo(formName), element);
            }
            else
            {
                Debug.LogError("不存在该Form的Prefab,请检查路径或名称是否正确!form name:" + formName);
            }
            return null;
        }
    }


}
